package com.eeepay.v2_library.api;



import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.util.Md5;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;




/**
 * TOP API请求业务参数
 * 
 */
public class Task {

	private Map<String, String> params = new HashMap<String, String>();
	private Map<String, FileItem> attachments = new HashMap<String, FileItem>();
	/** 任务的ID **/
	private int mTaskId;
	private String url;
	private String result;
	private boolean exception = false;
	private boolean isPost = false;
	
	public Task(String url,int taskId){
		this.url = url;
		this.mTaskId = taskId;
	}
	
	public Task(String url,int taskId,boolean isPost){
		this.url = url;
		this.mTaskId = taskId;
		this.isPost = isPost;
	}


	/**
	 * 添加业务参数
	 * 
	 * @param key
	 * @param value
	 */
	public void addParam(String key, String value) {
		params.put(key, value);
	}

	/**
	 * 获取已添加的业务参数
	 * 
	 * @param key
	 * @return
	 */
	public String getParam(String key) {
		return params.get(key);
	}

	/**
	 * 删除已添加的业务参数
	 * 
	 * @param key
	 */
	public void removeParam(String key) {
		params.remove(key);
	}

	/**
	 * 添加附件
	 * 
	 * @param key
	 * @param file
	 */
	public void addAttachment(String key, FileItem file) {
		if (file == null) {
			return;
		}
		attachments.put(key, file);
	}

	/**
	 * 获取已添加的附件
	 * 
	 * @param key
	 * @return
	 */
	public FileItem getAttachment(String key) {
		return attachments.get(key);
	}

	/**
	 * 删除已添加的附件
	 * 
	 * @param key
	 */
	public void removeAttachment(String key) {
		attachments.remove(key);
	}

	public Map<String, String> getParams() {
		return params;
	}

	public void setParams(Map<String, String> params) {
		this.params = params;
	}

	public Map<String, FileItem> getAttachments() {
		return attachments;
	}

	public void setAttachments(Map<String, FileItem> attachment) {
		this.attachments = attachment;
	}


	public String getResult() {
		return result;
	}


	public void setResult(String result) {
		this.result = result;
	}


	public boolean isException() {
		return exception;
	}


	public void setException(boolean exception) {
		this.exception = exception;
	}


	public int getmTaskId() {
		return mTaskId;
	}


	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}


	public boolean isPost() {
		return isPost;
	}


	public void setPost(boolean isPost) {
		this.isPost = isPost;
	}
	/**
	 *  实名认证的HMAC 校验，此方法请不要修改
	 *  @param prefix 前缀 固定传API(例如:实名认证传: API)
	 *  @param suffix 后缀 报文头大写(例如：实名认证传: REALNAMEAUTH)
	 *  @param params 加密数据
	 *  @return
	 *  @author zengja
	 *  @date 2015年12月23日 上午10:41:40
	 */
	public String getHMACM(String prefix,String suffix,Map<String,String> params){
		//按字典的键进行升序排序
		params = sortMapByKey(params);
		StringBuffer sb = new StringBuffer();
		sb.append("prefix").append(prefix);
		Set set = params.keySet();
		for (Object key : set) {
			if(!"hmac".equalsIgnoreCase(key.toString())){
				sb.append(key).append(params.get(key));
			}
		}
		sb.append("suffix").append(suffix);
		LogUtils.d("hmac前=" + sb.toString());
		LogUtils.d("hmac后=" + Md5.encode(Md5.encode(sb.toString())));
		//MD5两次计算
		return Md5.encode(Md5.encode(sb.toString()));
	}
	private Map sortMapByKey(Map map) {
		if (map == null || map.isEmpty()) {
			return null;
		}
		Map sortMap = new TreeMap(new Comparator<String>(){
			@Override
			public int compare(String o1, String o2) {
				return o1.compareTo(o2); //升序排列
			}
		});
		sortMap.putAll(map);
		return sortMap;
	}

}
